package com.rui.dao.impl;

import com.rui.dao.StudentSubjectDao;
import com.rui.entity.StudentSubject;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;

/**
 * Created by Administrator on 2017/4/6.
 */
@Repository
public class StudentSubjectDAOImpl implements StudentSubjectDao {
    @Autowired
    private SessionFactory sessionFactory;

    private Session getSession(){
        return sessionFactory.getCurrentSession();
    }

    public void add(StudentSubject studentSubject) {
        getSession().beginTransaction();
        getSession().save(studentSubject);
        getSession().getTransaction().commit();
    }

    public void delete(Integer id) {
        getSession().beginTransaction();
        StudentSubject studentSubject = (StudentSubject) getSession().get(StudentSubject.class, Integer.valueOf(id));
        if (studentSubject != null) {
            getSession().delete(studentSubject);
        }
        getSession().getTransaction().commit();
    }

    public void update(StudentSubject studentSubject) {
        getSession().beginTransaction();
        getSession().update(studentSubject);
        getSession().getTransaction().commit();
    }

    public StudentSubject findOne(Integer id) {
        getSession().beginTransaction();
        StudentSubject studentSubject = (StudentSubject) getSession().get(StudentSubject.class, Integer.valueOf(id));
        getSession().getTransaction().commit();
        return studentSubject;
    }

    public List<StudentSubject> findStudentSubjects(String realName, String subName, Integer pageSize, Integer pageIndex) {
        getSession().beginTransaction();
        StringBuilder sb = new StringBuilder("from StudentSubject ss where 1 = 1");
        if (StringUtils.isNotBlank(realName)) {
            sb.append(" and ss.student.realName like '%" + realName + "%'");
        }
        if (StringUtils.isNotBlank(subName)) {
            sb.append(" and ss.subject.name like '%" + subName + "%'");
        }
        Query query = getSession().createQuery(sb.toString());
        query.setFirstResult((pageIndex - 1) * pageSize);
        query.setMaxResults(pageSize);
        List<StudentSubject> studentSubjects = query.list();
        getSession().getTransaction().commit();
        return studentSubjects;
    }

    public Integer findTotalPage(String realName, String subName, Integer pageSize) {
        getSession().beginTransaction();
        StringBuilder sb = new StringBuilder("select count(id) from StudentSubject ss where 1 = 1");
        if (StringUtils.isNotBlank(realName)) {
            sb.append(" and ss.student.realName like '%" + realName + "%'");
        }
        if (StringUtils.isNotBlank(subName)) {
            sb.append(" and ss.subject.name like '%" + subName + "%'");
        }
        Query query = getSession().createQuery(sb.toString());
        Long count = (Long) query.uniqueResult();
        getSession().getTransaction().commit();
        if (count % pageSize == 0) {
            return count.intValue() / pageSize;
        } else {
            return count.intValue() / pageSize + 1;
        }
    }

    public Integer findCount(Integer stuId, Integer subId, Integer id) {
        getSession().beginTransaction();
        StringBuilder sb = new StringBuilder("select count(id) from StudentSubject where 1 = 1");
        if (stuId != null) {
            sb.append(" and student.id = " + stuId);
        }
        if (subId != null) {
            sb.append(" and subject.id = " + subId);
        }
        if (id != null) {
            sb.append(" and id != " + id);
        }
        Query query = getSession().createQuery(sb.toString());
        Long count = (Long) query.uniqueResult();
        getSession().getTransaction().commit();
        return count.intValue();
    }

    public List<StudentSubject> findPersonSubjects(Integer stuId, Integer pageSize, Integer pageIndex) {
        getSession().beginTransaction();
        StringBuilder sb = new StringBuilder("from StudentSubject where student.id = " + stuId);
        Query query = getSession().createQuery(sb.toString());
        query.setFirstResult((pageIndex - 1) * pageSize);
        query.setMaxResults(pageSize);
        List<StudentSubject> subjects = query.list();
        getSession().getTransaction().commit();
        return subjects;
    }

    public Integer findTotalPage(Integer stuId, Integer pageSize) {
        getSession().beginTransaction();
        StringBuilder sb = new StringBuilder("select count(id) from StudentSubject where student.id = " + stuId);
        Query query = getSession().createQuery(sb.toString());
        Long count = (Long) query.uniqueResult();
        getSession().getTransaction().commit();
        if (count % pageSize == 0) {
            return count.intValue() / pageSize;
        } else {
            return count.intValue() / pageSize + 1;
        }
    }
}
